package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.JsonResult;
import com.jy.wms.common.base.ManySheetOneSyler;
import com.jy.wms.pojo.SalesInquiriesOfProvincesAndCities;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsFreightStatisticsInVariousProvinces;
import com.jy.wms.pojo.wms_ReportQuery;
import com.jy.wms.service.ProvincialDynamicReportsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ProvincialDynamicReportsController")
@Api(value = "/ProvincialDynamicReportsController", description = "各省动态报表查询")
public class ProvincialDynamicReportsController extends BaseController<ProvincialDynamicReportsService, WmsFreightStatisticsInVariousProvinces> {

    @PostMapping(value = "StatisticsInVariousProvinces")
    @ApiOperation(value = "各省运费统计（按产品大类）", response = JSONObject.class, notes = "各省运费统计")
    public @ResponseBody
    JSONObject freightStatisticsInVariousProvinces(@RequestBody wms_ReportQuery wms_reportquery, HttpSession session) {
        JSONObject json = null;

        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();
        wms_reportquery.setHzid(hzid);
        wms_reportquery.setCkid(ckid);
        //取出数据
        json = baseService.freightStatisticsInVariousProvinces(wms_reportquery);
        return json;
    }

    //各省发货支数统计（按产品大类）
    @PostMapping(value = "Thenumberofshipmentsineach")
    @ApiOperation(value = "各省发货支数统计（按产品大类）", response = JSONObject.class, notes = "各省发货支数统计")
    public @ResponseBody
    JSONObject thenumberofshipmentsineachprovince(@RequestBody wms_ReportQuery wms_reportquery, HttpSession session) {
        JSONObject json = null;

        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();
        wms_reportquery.setHzid(hzid);
        wms_reportquery.setCkid(ckid);
        try {
            json = baseService.thenumberofshipmentsineachprovince(wms_reportquery);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    //各省销售额统计（按产品大类）
    @PostMapping(value = "Statisticsofsalesineach")
    @ApiOperation(value = "各省销售额统计（按产品大类）", response = JSONObject.class, notes = "各省销售额统计")
    public @ResponseBody
    JSONObject statisticsofsalesineachprovince(@RequestBody wms_ReportQuery wms_reportquery, HttpSession session) {
        JSONObject json = null;
        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();
        wms_reportquery.setHzid(hzid);
        wms_reportquery.setCkid(ckid);
        try {
            json = baseService.statisticsofsalesineachprovince(wms_reportquery);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    //各省订单量统计（按产品大类）
    @PostMapping(value = "Statisticsoforderquantityineach")
    @ApiOperation(value = "各省订单量统计（按产品大类）", response = JSONObject.class, notes = "各省订单量统计")
    public @ResponseBody
    JSONObject statisticsoforderquantityineachprovince(@RequestBody wms_ReportQuery wms_reportquery, HttpSession session) {
        JSONObject json = null;

        //获取当前操作用户的货主id
        List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = hz.get(0).getHzid();
        //获取仓库Id
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = userinfo.getUser().getOrgId().get(0).toString();
        wms_reportquery.setHzid(hzid);
        wms_reportquery.setCkid(ckid);
        try {
            json = baseService.statisticsoforderquantityineachprovince(wms_reportquery);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

        @PostMapping(value = "StatisticsInVarious")
        @ApiOperation(value = "各省运费统计（按产品大类）导出报表", response = JSONObject.class, notes = "各省运费统计导出报表")
        public @ResponseBody
        void baseSelectBathPrimaryKeys(@RequestBody WmsFreightStatisticsInVariousProvinces pojo, HttpServletResponse response, HttpServletRequest request, HttpSession session) throws IOException {

        try {
            //获取当前操作用户的货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = userinfo.getUser().getOrgId().get(0).toString();

            pojo.setCkid(ckid);
            pojo.setHzid(hzid);
            // 告诉浏览器用什么软件可以打开此文件
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("各省运费统计报表", "UTF-8") + ".xls");
            //编码
            response.setCharacterEncoding("UTF-8");
            ExportParams ep = new ExportParams();
            ep.setStyle(ManySheetOneSyler.class);

            List<ExcelExportEntity> Head = baseService.listHeader2(pojo);
            List<Map<String, Object>> FreightData = baseService.StatisticsInVariousProvinces(pojo);
            Workbook workbook = ExcelExportUtil.exportExcel(ep, Head, FreightData);
            workbook.write(response.getOutputStream());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping(value = "PmentSineachProvince")
    @ApiOperation(value = "各省支数统计（按产品大类）导出报表", response = JSONObject.class, notes = "各省支数统计导出报表")
    public @ResponseBody
    void pmentsineachprovince(@RequestBody SalesInquiriesOfProvincesAndCities pojo, HttpSession session, HttpServletResponse response) throws IOException {
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = userinfo.getUser().getOrgId().get(0).toString();

            pojo.setCkid(ckid);
            pojo.setHzid(hzid);
            // 告诉浏览器用什么软件可以打开此文件
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("各省支数统计报表", "UTF-8") + ".xls");
            //编码
            response.setCharacterEncoding("UTF-8");
            ExportParams ep = new ExportParams();
            ep.setStyle(ManySheetOneSyler.class);

            List<ExcelExportEntity> Head = baseService.zishuHeader(pojo);
            List<Map<String, Object>> PSCP=baseService.pmentsineachprovince(pojo);
            Workbook workbook = ExcelExportUtil.exportExcel(ep, Head, PSCP);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping(value = "StatisticsOfTheBranches")
    @ApiOperation(value = "各省销售额统计（按产品大类）导出报表", response = JSONObject.class, notes = "各省销售额统计导出报表")
    public @ResponseBody
    void StatisticsOfTheBranches(@RequestBody SalesInquiriesOfProvincesAndCities pojo, HttpSession session, HttpServletResponse response) throws IOException {
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = userinfo.getUser().getOrgId().get(0).toString();

            pojo.setCkid(ckid);
            pojo.setHzid(hzid);
            // 告诉浏览器用什么软件可以打开此文件
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("各省销售额统计报表", "UTF-8") + ".xls");
            //编码
            response.setCharacterEncoding("UTF-8");
            ExportParams ep = new ExportParams();
            ep.setStyle(ManySheetOneSyler.class);

            List<Map<String, Object>> SOTB=baseService.StatisticsOfTheBranches(pojo);
            List<ExcelExportEntity> Head = baseService.xseHeader(pojo);
            Workbook workbook = ExcelExportUtil.exportExcel(ep, Head, SOTB);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping(value = "OrderQuantityStatistics")
    @ApiOperation(value = "各省订单量统计（按产品大类）导出报表", response = JSONObject.class, notes = "各省销售额统计导出报表")
    public @ResponseBody
    void OrderQuantityStatistics(@RequestBody SalesInquiriesOfProvincesAndCities pojo, HttpSession session, HttpServletResponse response) throws IOException {
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid = hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = userinfo.getUser().getOrgId().get(0).toString();

            pojo.setCkid(ckid);
            pojo.setHzid(hzid);

            // 告诉浏览器用什么软件可以打开此文件
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("各省订单量统计报表", "UTF-8") + ".xls");
            //编码
            response.setCharacterEncoding("UTF-8");
            ExportParams ep = new ExportParams();
            ep.setStyle(ManySheetOneSyler.class);

            List<Map<String, Object>> ODQS=baseService.OrderQuantityStatistics(pojo);
            List<ExcelExportEntity> Head = baseService.ddlHeader(pojo);
            Workbook workbook = ExcelExportUtil.exportExcel(ep, Head, ODQS);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
